import {message, Tabs} from 'antd';
import React from 'react';
import {PageContainer} from '@ant-design/pro-layout';
import {isEmpty, isSuccess} from "@/utils/utils";
import {OpResourcesManager} from "@/services/api-functions";
import {SYS_ENUMS} from "@/utils/constants";
import {AdminPptResourcesModRequest, LabelDto, OssPojo} from "@/services/api-types";
import ResourceList from "@/pages/resource/list/component/ResourceList";

export const handleResourceEdit = async (fields: any, select: any, cover: OssPojo|undefined, paymentType: 'PAY'|'FREE',
                                         detailImage: OssPojo|undefined, previewsList: OssPojo[], resourcesAttachment: OssPojo|undefined, colors: LabelDto[], associated: any) => {
  try {
    if (paymentType === 'PAY' && (isEmpty(fields.sellingPrice) || isEmpty(fields.originalPrice))) {
      message.error(isEmpty(fields.sellingPrice) ? '请输入售价' : '请输入原价')
      return;
    }
    const rpo: AdminPptResourcesModRequest = {
      id: select?.id ?? '',
      attachmentSize: fields.attachmentSize,
      classification: fields.classification,
      cloudAttachment: fields.cloudAttachment,
      cover,
      detailImage,
      membersFree: fields.membersFree ? 1 : 0,
      originalPrice: fields.originalPrice,
      paymentType,
      previewsList: previewsList,
      resourcesAttachment,
      sellingPrice: fields.sellingPrice,
      title: fields.title,
      userId: fields.userId,
      labelList: fields.labels?.map((name: string)=>{return{id:'', name}}),
      colors
    }
    if (associated) {
      rpo.associatedId = associated.id;
      rpo.associatedType = associated.type;
    }
    if (isSuccess((select && select.id ? (await OpResourcesManager.resourcesEdit(rpo)) : (await OpResourcesManager.resourcesAdd(rpo)))?.code)) {
      message.success('保存成功');
      return true;
    }
    return false;
  } catch (error) {
    return false;
  }
};

const handleAudit = async (fields: any, select: any) => {
  if (!select) return false;
  try {
    if (isSuccess((await OpResourcesManager.resourcesAudit({
      businessId: select.id,
      auditOperate: fields.auditOperate,
      remark: fields.remark
    }))?.code)) {
      message.success(fields.auditOperate === SYS_ENUMS.RESOURCE_AUDIT_STATE.PASS.value ? '审核通过' : '审核拒绝');
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};

const handlePublishStatus = async (select: any, state: string) => {
  if (!select) return true;
  try {
    if (isSuccess((await OpResourcesManager.resourcesOperate(select.id, state))?.code)) {
      message.success('PUBLISHED' === state ? '发布成功' : '取消发布成功');
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};

export const handleDelete = async (select: any) => {
  if (!select) return false;
  try {
    message.loading('正在删除');
    if (isSuccess((await OpResourcesManager.resourcesDel(select.id))?.code)) {
      message.success('删除成功');
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};

export const resourceRecommend = async (select: any, recommend: number) => {
  if (!select) return false;
  try {
    if (isSuccess((await OpResourcesManager.resourcesRecommend(recommend, select.id))?.code)) {
      message.success('操作成功');
      return true;
    }
    return false
  } catch (error) {
    return false;
  }
};


const AllResourceList: React.FC<any> = (props) => {


  return (
    <PageContainer>
      <Tabs destroyInactiveTabPane={true}>
        <Tabs.TabPane forceRender tab='付费资源' key="PAY_RESOURCE">
          <ResourceList handlePublishStatus={handlePublishStatus} handleAudit={handleAudit} recommend={resourceRecommend}
                        {...props} handleDelete={handleDelete} type='PAY'/>
        </Tabs.TabPane>
        <Tabs.TabPane forceRender tab='免费资源' key="FREE_RESOURCE">
          <ResourceList handlePublishStatus={handlePublishStatus} handleAudit={handleAudit} recommend={resourceRecommend}
                        {...props} handleDelete={handleDelete} type='FREE'/>
        </Tabs.TabPane>
      </Tabs>
    </PageContainer>
  );
};

export default AllResourceList;
